package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.CwOtherIncomeRefund;
import com.kgc.collection.pojo.CwOtherIncomeRefundDetail;
import com.kgc.skProvider.dao.CwOtherIncomeRefundMapper;
import com.kgc.skProvider.service.CwOtherIncomeRefundService;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CwOtherIncomeRefundServiceImpl implements CwOtherIncomeRefundService {

    private Logger logger = LoggerFactory.getLogger(CwOtherIncomeRefundServiceImpl.class);

    @Autowired
    private CwOtherIncomeRefundMapper cwOtherIncomeRefundMapper;


    @Override
    public ResponseMsg selectCwOtherIncomeRefundByPage(Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundServiceImpl selectCwOtherIncomeRefundByPage params {}",map.toString());
        ResponseMsg responseMsg =new ResponseMsg();
        PageInfo<CwOtherIncomeRefund> pageInfo=new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<CwOtherIncomeRefund> list=cwOtherIncomeRefundMapper.selectCwOtherIncomeRefundByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("CwOtherIncomeRefundServiceImpl selectCwOtherIncomeRefundByPage return {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectCwOtherIncomeRefundByCode(Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundServiceImpl selectCwOtherIncomeRefundByCode params {}",map.toString());
        ResponseMsg responseMsg =new ResponseMsg();
        CwOtherIncomeRefund cwOtherIncomeRefund=cwOtherIncomeRefundMapper.selectCwOtherIncomeRefundByCode(map);
        List<CwOtherIncomeRefundDetail> list=cwOtherIncomeRefundMapper.selectCwOtherIncomeRefundDetail(map);
        cwOtherIncomeRefund.setDetails(list);
        responseMsg.setData(cwOtherIncomeRefund);
        logger.debug("CwOtherIncomeRefundServiceImpl selectCwOtherIncomeRefundByCode return {}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateCwOtherIncomeRefund(Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundServiceImpl updateCwOtherIncomeRefund params {}",map.toString());
        deleteCwOtherIncomeRefund(map);
        ResponseMsg responseMsg = insertCwOtherIncomeRefund(map);
        logger.debug("CwOtherIncomeRefundServiceImpl updateCwOtherIncomeRefund return {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertCwOtherIncomeRefund(Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundServiceImpl insertCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg =new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        CwOtherIncomeRefund cwOtherIncomeRefund=mapper.convertValue(map.get("cwOtherIncomeRefund"), CwOtherIncomeRefund.class);
        List<?> rawDetails=(List<?>)map.get("details");
        List<CwOtherIncomeRefundDetail> details=mapper.convertValue(rawDetails,new TypeReference<List<CwOtherIncomeRefundDetail>>(){});
        int result=cwOtherIncomeRefundMapper.insertCwOtherIncomeRefund(cwOtherIncomeRefund);
        try{
            for (CwOtherIncomeRefundDetail detail : details) {
                cwOtherIncomeRefundMapper.insertCwOtherIncomeRefundDetail(detail);
            }
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("CwOtherIncomeRefundServiceImpl insertCwOtherIncomeRefund return {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteCwOtherIncomeRefund(Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundServiceImpl deleteCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg =new ResponseMsg();
        int result1 = cwOtherIncomeRefundMapper.deleteCwOtherIncomeRefund(map);
        int result2=cwOtherIncomeRefundMapper.deleteCwOtherIncomeRefundDetail(map);
        if(result1>0 && result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("CwOtherIncomeRefundServiceImpl deleteCwOtherIncomeRefund return {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg checkCwOtherIncomeRefund(Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundServiceImpl checkCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg =new ResponseMsg();
        int result=cwOtherIncomeRefundMapper.checkCwOtherIncomeRefund(map);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("CwOtherIncomeRefundServiceImpl checkCwOtherIncomeRefund return {}",responseMsg);
        return responseMsg;
    }
}
